// 流程发起的hook

import { cloneDeep } from 'lodash'

export const useFlowStart = (selectData, initOption) => {
  // 发起流程的类型
  const flowStartType = ref('same')
  const flowStartVisible = ref(true)
  const flowStartDataList = ref([])
  const flowStartBtnLoading = ref(false)
  // 流程发起事件（单选、多选）
  // selectType: 单选触发还是多选触发
  // row单选行数据
  // startType多选的类型 （一条流程还是多条流程）
  const handleFlowCommand = async (selectType, row, startType) => {
    if (selectType === 'radio') {
      flowStartDataList.value = cloneDeep([row])
      flowStartType.value = 'same'
      row._flowStartloading = true
    } else if (selectType === 'multiple') {
      flowStartDataList.value = cloneDeep(selectData.value)
      flowStartType.value = startType.value
      flowStartBtnLoading.value = true
    }
    // 流程发起校验 (没有校验函数或者校验函数返回true则表示通过校验)
    let flag =
      !initOption.value.flowStartVerifyFun ||
      (await initOption.value.flowStartVerifyFun(flowStartDataList.value))
    // 关闭loading
    flowStartBtnLoading.value = false
    row && (row._flowStartloading = false)
    flowStartVisible.value = !!flag
  }
  return {
    flowStartDataList,
    flowStartVisible,
    flowStartBtnLoading,
    handleFlowCommand,
  }
}
